package com.itqf.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * projectName: mybatisgp8
 *
 * @author: 赵伟风
 * time: 2021/7/29 16:38
 * description:
 *
 *    sqlsessionfactoryBuidler  用完就扔, 方法内部!
 *    sqlsessionfactory  一直存在,创建一次!
 *    sqlsession  一个线程 一次请求 一个方法内部
 *    mapper映射器 一个线程 一次请求 一个方法内部
 *
 */
public class SqlSessionUtils {


    public static  SqlSessionFactory sqlSessionFactory = null;

    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

    }

    public static SqlSession openSession(){
        return openSession(false);
    }

    public static SqlSession openSession(boolean autoCommit){
        return sqlSessionFactory.openSession(autoCommit);
    }


}
